package com.example.springbootvuetest.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootvuetest.entity.Favorite;
import com.example.springbootvuetest.entity.IdleItem;
import com.example.springbootvuetest.entity.User;
import com.example.springbootvuetest.mapper.IdleItemMapper;
import com.example.springbootvuetest.mapper.UserMapper;
import com.example.springbootvuetest.service.IIdleItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shuoye
 * @since 2023-03-14
 */
@Service
public class IdleItemServiceImpl extends ServiceImpl<IdleItemMapper, IdleItem> implements IIdleItemService {

    @Autowired
    private IdleItemMapper idleItemMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean updateIdleStatus(Integer id, Byte status) {
        boolean b = idleItemMapper.updateIdleStatus(id, status);
        return b;
    }

    @Override
    public List<IdleItem> getAllIdleItem(Integer userId) {
        QueryWrapper<IdleItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).orderByDesc("release_time");
        return idleItemMapper.selectList(queryWrapper);
    }

    @Override
    public IdleItem getIdleItem(Integer id) {
        IdleItem idleItem = idleItemMapper.selectById(id);
        if (idleItem != null)
            idleItem.setUser(userMapper.selectById(idleItem.getUserId()));
        return idleItem;
    }

    @Override
    public Map<String, Object> findIdleItem(String findValue, int pageNum, int pageSize, int status, Integer idleLabel) {
        Map<String, Object> resMap = new HashMap<>();
        List<IdleItem> list = idleItemMapper.findIdleItem(findValue, (pageNum - 1) * pageSize, pageSize, status, idleLabel);
//        for (IdleItem i:list) {
//            System.out.println(i.getIdleName() + ' ' + i.getIdleStatus() + '\n');
//        }

        if (list.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for (IdleItem i : list) {
                idList.add(i.getUserId());
            }
            List<User> userList = userMapper.selectBatchIds(idList);
            Map<Integer, User> map = new HashMap<>();
            for (User user : userList) {
                map.put(user.getId(), user);
            }
            for (IdleItem i : list) {
                i.setUser(map.get(i.getUserId()));
            }
        }
        int count;
        if (StrUtil.isBlank(findValue))
            count = idleItemMapper.countIdleItemByLable(idleLabel);
        else {
            count = idleItemMapper.countIdleItem(findValue);
        }
        resMap.put("data", list);
        resMap.put("total", count);
        return resMap;
    }

    @Override
    public Map<String, Object> findIdleByList(List<Integer> idList) {
        return null;
    }

    @Override
    public Map<String, Object> getAdminIdleList(Integer pageNum, Integer pageSize, String idleName, Integer status) {
        HashMap<String, Object> resmap = new HashMap<>();
        IPage<IdleItem> ipage = new Page<>(pageNum, pageSize);
        QueryWrapper<IdleItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("idle_status", status);
        if (!idleName.equals(""))
            queryWrapper.like("idle_name", idleName);
        IPage<IdleItem> iPage = idleItemMapper.selectPage(ipage, queryWrapper.orderByAsc("id"));
        List<IdleItem> list = iPage.getRecords();
        if (list.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for (IdleItem i : list) {
                idList.add(i.getUserId());
            }
            List<User> userList = userMapper.selectBatchIds(idList);
            Map<Integer, User> map = new HashMap<>();
            for (User user : userList) {
                map.put(user.getId(), user);
            }
            for (IdleItem i : list) {
                i.setUser(map.get(i.getUserId()));
            }
        }
        int count = idleItemMapper.countIdleItemByStatus(status);
        resmap.put("data", iPage.getRecords());
        resmap.put("total", iPage.getTotal());
        return resmap;
    }

}
